<?php 
//create by fu
namespace app\api\model;

use app\api\model\BaseModel;

/**
 * 
 */
class Product extends BaseModel
{
	//隐藏字段
	protected $hidden = ['delete_time','category_id','img_id','from','create_time','update_time','privot'];
	
	public function getMainImgUrlAttr($value,$data)
	{
	 	return $this->PrefixImgUrl($value,$data);
	}

	//关联模型
	public function imgs()
	{
		// 一对多 hasMany(从表，从表字段，本表字段)
		return $this->hasMany('product_image','product_id','id');
	}
	public function Properties()
	{
		// 一对多 hasMany(从表，从表字段，本表字段)
		return $this->hasMany('product_property','product_id','id');
	}

	public static function getMostRecent($count)
	{
		$products = self::limit($count)
		          ->order("create_time","desc")
		          ->select();

        return $products;
	}
    
	public static function getProductsByCategoryID($categoryID)
	{
		$products = self::where('category_id','=',$categoryID)
		                ->order('create_time','desc')
		                ->select();
		return $products;
	}
    //获取商品详情
	public static function getProductDetail($id)
	{
		$products = self::with([
			            'imgs'=>function($query){
			            	$query->with(['imgUrl']);
			            }

	                 ])
		                ->with(['Properties'])
		                ->find($id);

		return $products;
	}


	 
}




 ?>